function Bola(){
	this.x = barra.x + barra.w/2;
	this.y = barra.y - this.h;
	this.vx = 0;
	this.vy = 0;
	this.w = 20;
	this.h = 20;
	this.started = false;
	this.timeColBarra;
	this.auxColBarra = false; // variable auxiliar para el timeout de colision con barra. 
							  // False = no ha tocado la barra, true = si la a tocado y durante 1s no la podra tocar
	this.audio = new Audio();
	this.audio.src = "http://www.lunerouge.org/sons/musique/LRTada%2003%20by%20Lionel%20Allorge.ogg";
}

Bola.prototype = {
	start: function(){
		if(this.started == false){
			this.started = true;
			this.vx = Math.random()*5;
			this.vy = 0;
			if(this.vx  < vConstante){
				this.vy = Math.sqrt((this.vx - vConstante) * -1)*-1;
			}else{
				this.vy = Math.sqrt(this.vx - vConstante);
			}			
		}	
	},
	move: function() {			
		this.x += this.vx * dificultad * efecto;
		this.y += this.vy * dificultad * efecto;
	},
	
	moveEnBarra : function(){
		if(this.started == false){
			this.x = barra.x + barra.w/2;
			this.y = barra.y - this.h;
		}
	},
	
	colisiona : function(){
		
		//COLISION CON LA BARRA
		rnd = Math.random() * 4;
		if(colisionObjets(this,barra,0)== true && this.auxColBarra == false ){
			this.trueCol();	
			if(this.x + this.w/2 > barra.x + (barra.w/4*3 )){
				this.vx = 5 + rnd;
			}else if(this.x + this.w/2 > barra.x + (barra.w/4*2)){
				this.vx = 3 + rnd;
			}else if(this.x + this.w/2 > barra.x + (barra.w)){
				this.vx = (3 + rnd) * -1;
			}else{
				this.vx = (5 + rnd) * -1;
			}
			if(this.vy>0){
				this.vy = this.vy *-1;
			}
		}
		
		//COLISION CON LAS PAREDES
		coliPared=colisionParedes(this,500,500,3);
		if(coliPared != 0){
			if(coliPared==1){
				if(this.vy < 0){
					this.vy= this.vy * -1;
				}
			}
			if(coliPared==2){
				if(this.vx > 0){
					this.vx = this.vx * -1;
				}else{
					this.vx = this.vx;
				}				
			}
			
			if(coliPared==4){				
				if(this.vx > 0){
					this.vx = this.vx;
				}else{
					this.vx = this.vx * -1;
				}
			}			
		}
		//COLISION BOLA MUERTA(Toca fondo)
		if(this.y + this.h +5 > 800){
			bolas.splice(bolas.indexOf(this),1);
			if(bolas.length== 0){
				game = 0;
				this.audio.play();
				guardarPuntos();
			}
		}
		
		//COLISIONES CON BLOQUES
		for(var x=0; x< blocks.length;x++){
			if(colisionObjets(this,blocks[x],3)){
				if(this.x + this.w/2 < blocks[x].x + blocks[x].h){
					this.vx = this.vx ;
				}else{
					this.vx = this.vx ;
				}
					this.vy = this.vy *-1;
					blocks[x].deleteBlock();
			}
		}
		
	},
	
	trueCol : function(){
		this.auxColBarra = true;	
		this.timeColBarra = setInterval(falseCol(this), 500);
	},


}

function falseCol(bola) {
	bola.auxColBarra = false;
	bola.timeColBarra=window.clearInterval(bola.timeColBarra);
}